package com.xuzimian.globaldemo.mq.rabbitmq.spring;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author xzm
 * @Description spring rabbitmq 使用用列
 */
public class SpringRabbitMQDemo {


    @BeforeEach()
    public void sendMessage(){
        AbstractApplicationContext ctx =
                new ClassPathXmlApplicationContext("spring-rabbit.xml");
        RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
        template.convertAndSend("Hello World");
        ctx.close();
        System.out.println("推送成功");
    }

    @Test
    public void receiveMessage(){
        AbstractApplicationContext ctx =
                new ClassPathXmlApplicationContext("spring-rabbit.xml");
        RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
        Message msg= template.receive("default_queue");
        //消息会被SimpleMessageListener接收去
        if(null != msg){
            System.out.println(new String(msg.getBody()));
        }
        ctx.close();
    }
}
